import { createRouter, createWebHashHistory } from 'vue-router'
import OCRView from '../views/OCRView.vue'
// import ExcelAnalysisView from '../views/ExcelAnalysis.vue'
import ExcelImportView from '../views/ExcelImportView.vue'
// import SettingsView from '../views/SettingsView.vue'
import LoginView from '../views/LoginView.vue'
import PhotoFormView from '../views/NewsPhotoView.vue'
import PersonPositionListView from '../views/PersonPositionListView.vue'
import MedicalInsuranceListView from '../views/MedicalInsuranceListView.vue'
import NewsPhotoListView from '../views/NewsPhotoListView.vue'
import UserManagementView from '../views/UserManagementView.vue'
import UnitManagementView from '../views/UnitManagementView.vue' // 新增导入
import { useUserStore } from '@/stores/user'
import { Document, Picture, MessageBox, User, Files } from '@element-plus/icons-vue'
const isElectron = navigator.userAgent.includes('Electron')

const routes = [
  {
    path: '/',
    redirect: '/person-position'
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/news-photo-list',
    name: 'NewsPhotoList',
    component: NewsPhotoListView,
    meta: {
      requiresAuth: true,
      keepAlive: true,
      menu: {
        title: '新闻档案',
        icon: Files,
        show: true
      }
    }
  },
  {
    path: '/ocr',
    name: 'OCR',
    component: OCRView,
    meta: {
      requiresAuth: true,
      keepAlive: true,
      menu: {
        title: 'OCR识别',
        icon: Document,
        show: isElectron
      }
    }
  },
  {
    path: '/news-photo',
    name: 'NewsPhoto',
    component: PhotoFormView,
    meta: {
      requiresAuth: true,
      keepAlive: true,
      menu: {
        title: '新闻照片',
        icon: Picture,
        show: true
      }
    }
  },
  {
    path: '/user-management',
    name: 'UserManagement',
    component: UserManagementView,
    meta: {
      requiresAuth: true,
      keepAlive: false,
      menu: {
        title: '用户管理',
        icon: User,
        show: true
      }
    }
  },
  {
    path: '/unit-management',
    name: 'UnitManagement',
    component: UnitManagementView,
    meta: {
      requiresAuth: true,
      keepAlive: false,
      menu: {
        title: '单位管理',
        icon: User,
        show: true
      }
    }
  },
  {
    path: '/medical-records',
    name: 'MedicalInsuranceList',
    component: MedicalInsuranceListView,
    meta: {
      requiresAuth: true,
      keepAlive: false,
      menu: {
        title: '医保档案',
        icon: Files,
        show: true
      }
    }
  },
  {
    path: '/excel-import',
    name: 'ExcelImport',
    component: ExcelImportView,
    meta: {
      requiresAuth: true,
      keepAlive: true,
      menu: {
        title: 'Excel导入',
        icon: MessageBox,
        show: true
      }
    }
  },
  {
    path: '/person-position',
    name: 'PersonPosition',
    component: PersonPositionListView,
    meta: {
      requiresAuth: true,
      keepAlive: false,
      menu: {
        title: '姓名职务',
        icon: User,
        show: true
      }
    }
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 检查目标路由是否需要认证
  if (to.meta.requiresAuth) {
    // 有 token 且未过期
    if (userStore.token) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 记录原路径，登录后跳转
      })
    }
  } else {
    next() // 不需要认证的路由直接放行
  }
})

export default router